package cn.support.neo.mvp.core.service.intercaptor;

import android.text.TextUtils;

import com.github.pwittchen.prefser.library.Prefser;

import java.io.IOException;

import cn.support.neo.mvp.core.base.Constants;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Name:设置Cookie的拦截器
 * Author: WK（wangkun@xjzhicheng.com)
 * Date: 2016-06-30
 * Describe: PLEASE TO FIXME...
 */
public class SetCookieInterceptor implements Interceptor {

    Prefser prefser;

    public SetCookieInterceptor(Prefser prefser) {
        this.prefser = prefser;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        String cookie = prefser.get(Constants.GUEST_SESSION_KEY, String.class, "");
        Request request = chain.request();
        if (TextUtils.isEmpty(cookie)) {
            return chain.proceed(request);
        } else {
            request = request.newBuilder()
                    .header("Cookie",cookie.substring(0, cookie.length() - 1))
                    .build();
            return chain.proceed(request);
        }
    }
}
